﻿package com.yy.ui.controls {
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.net.*;
    import com.fminutes.vinciUI.core.*;

    public class Image extends UISprite {

        public static const LOAD_COMPLETE_EVENT:String = "loadCompleteEvent";
        public static const CLIP:String = "clip";
        public static const SCALE:String = "scale";
        public static const AUTO:String = "auto";

        private var _source:Object;
        private var _loader:Loader;
        private var _defaultImage:DisplayObject;
        private var _sizeType:String = "clip";
        private var _width:Number = 50;
        private var _height:Number = 50;

        public function get source():Object{
            return (this._source);
        }
        public function set source(_arg1:Object):void{
            if (this._source == _arg1){
                return;
            };
            this._source = _arg1;
            if (this._source == null){
                this.hideLoader();
                return;
            };
            if ((((this._source is String)) && (!((this._source == ""))))){
                this.showLoader();
                if (this._loader == null){
                    this._loader = new Loader();
                    this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onLoadCompletedHandler);
                    this._loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.onIoError);
                    addChild(this._loader);
                };
                this._loader.load(new URLRequest((_arg1 as String)));
            } else {
                this.hideLoader();
            };
        }
        private function showLoader():void{
            if (this._loader){
                this._loader.visible = true;
            };
            if (this.defaultImage){
                this.defaultImage.visible = false;
            };
        }
        private function hideLoader():void{
            if (this._loader){
                this._loader.visible = false;
            };
            if (this.defaultImage){
                this.defaultImage.visible = true;
            };
        }
        private function onLoadCompletedHandler(_arg1:Event):void{
            if (this.defaultImage){
                if ((((this._loader.width == 0)) || ((this._loader.height == 0)))){
                    this.defaultImage.visible = true;
                } else {
                    this.defaultImage.visible = false;
                };
            };
            this.setSize();
            this.dispatchEvent(new Event(Image.LOAD_COMPLETE_EVENT));
        }
        private function onIoError(_arg1:IOErrorEvent):void{
        }
        private function setSize():void{
            if (this._loader == null){
                return;
            };
            switch (this.sizeType){
                case Image.CLIP:
                    this._loader.scaleX = (this._loader.scaleY = 1);
                    this._loader.scrollRect = new Rectangle(0, 0, this.width, this.height);
                    break;
                case Image.SCALE:
                    this._loader.scrollRect = null;
                    this._loader.width = this.width;
                    this._loader.height = this.height;
                    break;
                case Image.AUTO:
                    this._loader.scaleX = (this._loader.scaleY = 1);
                    this._loader.scrollRect = null;
                    break;
            };
        }
        public function get defaultImage():DisplayObject{
            return (this._defaultImage);
        }
        public function set defaultImage(_arg1:DisplayObject):void{
            if (((!((this._defaultImage == null))) && (this.contains(this._defaultImage)))){
                this.removeChild(this._defaultImage);
            };
            if (_arg1 != null){
                addChildAt(_arg1, 0);
            };
            this._defaultImage = _arg1;
        }
        public function get sizeType():String{
            return (this._sizeType);
        }
        public function set sizeType(_arg1:String):void{
            this._sizeType = _arg1;
            this.setSize();
        }
        override public function set width(_arg1:Number):void{
            this._width = _arg1;
            this.setSize();
        }
        override public function get width():Number{
            if ((((this.sizeType == Image.AUTO)) && (!((this._loader == null))))){
                return (this._loader.width);
            };
            return (this._width);
        }
        override public function set height(_arg1:Number):void{
            this._height = _arg1;
            this.setSize();
        }
        override public function get height():Number{
            if ((((this.sizeType == Image.AUTO)) && (!((this._loader == null))))){
                return (this._loader.height);
            };
            return (this._height);
        }

    }
}